import os
import sys
import xml.dom.minidom

SET_ETYMON = {}
SET_ETYMON['dir'] = '''D:\Code\hg_local\word_list\input\etymologies'''
SET_ETYMON['parser'] = '''parseEtymon'''

SET_FAMILY = {}
SET_FAMILY['dir'] = '''D:\Code\hg_local\word_list\input\word_families'''
SET_FAMILY['parser'] = '''parseFamily'''

SET_WORDSETS = {}
SET_WORDSETS['dir'] = '''D:\Code\hg_local\english-learning-tools\grabber\input\word_sets'''
SET_WORDSETS['parser'] = '''parseWordset'''

SET_LDOCE5 = {}
SET_LDOCE5['dir'] = '''E:\LDOCE5_Depress\\fs.skn\\files.skn'''
SET_LDOCE5['parser'] = '''parseLDOCE5'''

SET_OALD8 = {}
SET_OALD8['dir'] = '''E:\OALD8_Depress\\fs.skn\\files.skn'''
SET_OALD8['parser'] = '''parseOALD8'''


def getXMLs(dirname):
    files = []
    while 1:
        root = dirname
        for parent,dirNames,fileNames in os.walk(root):
            for dirName in dirNames:
                None
            for fileName in fileNames:
                try:
                    fileName.index('.xml')
                    files.append(os.path.join(parent,fileName))
                except ValueError:
                    None
        break
    return files

def generalParser(filename):
    f = open(filename)
    content = f.read()
    f.close()
    dom = xml.dom.minidom.parseString(content)
    entity = dom.documentElement
    idm_id = entity.getAttribute('idm_id')
    id = entity.getAttribute('id')
    return (dom, idm_id, id)

def parseFamily(filename):
    dom, idm_id, id = generalParser(filename)
    family = [idm_id, id]
    refs = dom.getElementsByTagName('Ref')
    for ref in refs: family.append(ref.firstChild.data)
    return family

def parseWordset(filename):
    dom, idm_id, id = generalParser(filename)
    family = [idm_id, id]
    for ref in dom.getElementsByTagName('name'): family.append(ref.firstChild.data)
    for ref in dom.getElementsByTagName('number'): family.append(ref.firstChild.data)
    return family

def parseLDOCE5(filename):
    dom, idm_id, id = generalParser(filename)
    ldoce5 = [idm_id, id]
    refs = dom.getElementsByTagName('BASE')
    for ref in refs:
        ldoce5.append(ref.firstChild.data)
        break
    freqs = []
    for freq in dom.getElementsByTagName('FREQ'):
        freqs.append(freq.lastChild.data)
    for freq in dom.getElementsByTagName('AC'):
        freqs.append(freq.lastChild.data)
    ldoce5.append(','.join(freqs))
    return ldoce5

def parseOALD8(filename):
    dom, idm_id, id = generalParser(filename)
    oald8 = [idm_id, id]
    freqs = []
    if dom.documentElement.getAttribute('core') : freqs.append('core')
    if dom.documentElement.getAttribute('academic') : freqs.append('academic')
    oald8.append(','.join(freqs))
    return oald8

def parseEtymon(filename):
    dom, idm_id, id = generalParser(filename)
    hwd = dom.getElementsByTagName('HWD')[0].firstChild.data
    etymon = [idm_id, id, hwd]
    refs = dom.getElementsByTagName('Ref')
    for ref in refs: etymon.append(ref.getAttribute('topic'))
    return etymon

def dumpIndex(dirname, index):
    f = open(dirname+'\\index', 'wb')
    for i in index: f.write((u'\t'.join(i)+u'\n').encode('utf-8'))
    f.close()
    f = open(dirname+'.index', 'wb')
    for i in index: f.write((u'\t'.join(i)+u'\n').encode('utf-8'))
    f.close()

def buildIndex(setConfig):
    f = getattr(__import__(__name__), setConfig['parser'])
    dirname = setConfig['dir']
    while dirname[-1] == '\\':
        dirname = dirname[:-1]
    dumpIndex(dirname, map(f, getXMLs(dirname)))

if __name__ == '__main__':
    #buildIndex(SET_FAMILY)
    #buildIndex(SET_ETYMON)
    #buildIndex(SET_OALD8)
    #buildIndex(SET_LDOCE5)
    #buildIndex(SET_WORDSETS)
